package hg.framework.common.controller.result;


import lombok.Data;

import java.io.Serializable;

/**
 * 响应结果封装
 * @param <T>
 */
@Data
public class R<T> implements Serializable {


    private static final int CODE_SUCCESS = 0;

    /**
     * 响应结果[0: 成功,其他: 失败]
     */
    private int code;
    /**
     * 描述
     */
    private String msg;
    /**
     * 业务结果,code:为0时有值
     */
    private T data;

    public void ok(T data) {
        this.ok("成功", data);
    }

    public void ok(String msg, T data) {
        this.code = CODE_SUCCESS;
        this.msg = msg;
        this.data = data;
    }

    public void failed(String errMsg) {
        this.failed(-1, errMsg);
    }

    public void failed(int code, String errMsg) {
        this.code = code;
        this.msg = errMsg;
    }

    public boolean isSucc() {
        return CODE_SUCCESS == this.code;
    }
}
